﻿// LkEnsemblerProgram.cs
// Copyright 2009 Edgar Said Hernández Sánchez (Limo Wan Kenobi)
// limowankenobi@gmail.com
// http://lnkarel.sourceforge.net/
// http://limotonterias.blogspot.com/

/**
 * This file is part of LNKarel.
 *  
 * LNKarel is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * LNKarel is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with LNKarel.  If not, see <http://www.gnu.org/licenses/>.
 **/

using System;
using System.Collections.Generic;

namespace LMSoft.NKarel.Core
{
    /// <summary>
    /// Clase que será un programa de Karel.
    /// </summary>
    public class LkEnsemblerProgram
    {
        private const int DEFAULT_MAX_INSTRUCTIONS_ALLOWED = 10000;

        private int apuntador;

        private int pasosDados;

        Stack<int> functionStack = new Stack<int>();

        Stack<int> iterateStack = new Stack<int>();

        private List<LkEnsambler> Program
        {
            get;
            set;
        }

        public int CantInstructions
        {
            get
            {
                return Program.Count;
            }
        }

        public bool ExecutionTerminated
        {
            get
            {
                if (apuntador < Program.Count)
                {
                    return Program[apuntador] == LkEnsambler.TurnOff;
                }

                return true;
            }
        }

        private int MayorCantidadDePasos
        {
            get;
            set;
        }

        public LkEnsemblerProgram()
            : this(new List<LkEnsambler>())
        {
        }

        public LkEnsemblerProgram(List<LkEnsambler> program)
            : this(program, DEFAULT_MAX_INSTRUCTIONS_ALLOWED)
        {
        }

        public LkEnsemblerProgram(List<LkEnsambler> program, int maxInstructionsAllowed)
        {
            Program = program;
            MayorCantidadDePasos = maxInstructionsAllowed;
            RestartProgram();
        }

        public void RestartProgram()
        {
            apuntador = 0;
            pasosDados = 0;
            functionStack.Clear();
            iterateStack.Clear();
        }

        public LkEnsambler NextInstruction()
        {
            LkEnsambler ret = LkEnsambler.Nada;

            if (pasosDados > MayorCantidadDePasos)
            {
                throw new LkProgramException("Se han dado más pasos de los permitidos.");
            }

            if (!ExecutionTerminated)
            {
                if (apuntador < CantInstructions)
                {
                    // FIXTAG!!!! calcular la siguiente instruccion
                    apuntador++;

                    ret = Program[apuntador];
                }
                else
                {
                    throw new LkProgramException("El programa cayó en un estado no válido.");
                }
            }
            else
            {
                throw new LkExecutionProgramException("Ya se finalizó la ejecución del programa.");
            }

            return ret;
        }
    }
}
